package com.erhuogo.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.erhuogo.dao.SchoolDao;
import com.erhuogo.entity.School;
import com.erhuogo.util.DateUtils;

/**
 * 高校Dao的实现类
 * @author aibinxiao
 * @date 2016年10月26日 上午11:48:59
 */
public class SchoolDaoImpl extends HibernateDaoSupport implements SchoolDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(SchoolDaoImpl.class);

	// 1.根据城市Id获取所有的高校Dao实现类
	@Override
	public List<School> findCitysByProId(Integer cityId) {
		String hql = "from School where cityId = ? and status = ?";
		List<School> result = null;
		List<School> list = this.getHibernateTemplate().find(hql, cityId, 0);
		if(list != null && list.size() > 0){
			logger.info("所有高校信息集合：" + list);
			result = list;
		}
		return result;
	}

	// 2.根据高校id获取高校信息Dao实现类
	@Override
	public School findSchoolById(Integer schoolId) {
		String hql = "from School where id = ?";
		School result = null;
		List<School> list = this.getHibernateTemplate().find(hql, schoolId);
		if(list != null && list.size() > 0){
			logger.info("根据高校id获取高校信息：" + list.get(0));
			result = list.get(0);
		}
		return result;
	}

	// 3.查询符合条件的高校记录条数Dao实现类
	@Override
	public int countByParams(String id, String name, String startDate, String endDate, String provinceIdStr,
			String cityIdStr) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(School.class);
		if(!StringUtils.isEmpty(provinceIdStr)){
			Integer provinceId = Integer.valueOf(provinceIdStr);
			criteria.add(Restrictions.eq("provinceId", provinceId));
		}
		if(!StringUtils.isEmpty(cityIdStr)){
			Integer cityId = Integer.valueOf(cityIdStr);
			criteria.add(Restrictions.eq("cityId", cityId));
		}
		if(!StringUtils.isEmpty(id)){
			Integer threeCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(threeCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<School> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的高校");
		} else{
			logger.info("没有找到符合查询条件求的高校");
		}
		return count;
	}

	// 4.查询符合条件的高校记录集合Dao实现类
	@Override
	public List<School> findPageByParams(String id, String name, String startDate, String endDate, String provinceIdStr,
			String cityIdStr, int startIndex, int records) {
		List<School> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(School.class);
		if(!StringUtils.isEmpty(provinceIdStr)){
			Integer provinceId = Integer.valueOf(provinceIdStr);
			criteria.add(Restrictions.eq("provinceId", provinceId));
		}
		if(!StringUtils.isEmpty(cityIdStr)){
			Integer cityId = Integer.valueOf(cityIdStr);
			criteria.add(Restrictions.eq("cityId", cityId));
		}
		if(!StringUtils.isEmpty(id)){
			Integer threeCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(threeCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<School> list = this.getHibernateTemplate().findByCriteria(criteria, startIndex, records);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ result.size() +")个符合查询条件的高校");
		} else{
			logger.info("没有找到符合查询条件求的高校");
		}
		return result;
	}
	
	// 5.更新或者保存高校信息Dao实现类
	@Override
	public void saveSchool(School school) throws Exception{
		this.getHibernateTemplate().saveOrUpdate(school);
	}

	// 6.更新高校的启用状态Dao实现类
	@Override
	public void updateStauts(Integer id, Integer statusCode) throws Exception{
		String hql = "from School where id = ?";
		List<School> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			School school = list.get(0);
			school.setStatus(statusCode);
			school.setAlterTime(DateUtils.DateToStr(new Date()));
			this.getHibernateTemplate().saveOrUpdate(school);
		}
	}
}
